import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  //存放数据信息
  state:{
      token:{},
      user:{},
      tid: "",
      pid: "",
      newPirce: "",
  },
    getters:{
        getTid : state=>{
            let tid = state.tid;
            if(!tid.length){
                tid = window.sessionStorage.getItem('tid');
            }
            return tid;
        },
        getPid : state=>{
            let pid = state.pid;
            if(!pid.length){
                pid = window.sessionStorage.getItem('pid');
            }
            return pid;
        },
        getNewPirce : state=>{
            let newPirce = state.newPirce;
            if(!newPirce.length){
                newPirce = window.sessionStorage.getItem('newPirce');
            }
            return newPirce;
        },
    },
    //数据操作通道
    mutations:{
        setNewPirce(state,newPirce){
            state.newPirce=newPirce;
            sessionStorage.setItem("newPirce",newPirce);
        },
        setTid : (state,tid) => {
            state.tid = tid;
            window.sessionStorage.setItem('tid',tid);
        },
        setPid : (state,pid) => {
            state.pid = pid;
            window.sessionStorage.setItem('pid',pid);
        },
        setToken(state,token){
          state.token=token;
          sessionStorage.setItem("token",token);
        },
        removeToken(state,token){
          state.token = "";
          sessionStorage.removeItem("token");
        }
    }
})
